<?php

namespace app\api\controller\bargain;

use app\api\controller\Controller;
use app\api\model\bargain\Setting;
use app\api\model\Goods as GoodsModel;
use app\api\model\bargain\Active as ActiveModel;
use app\api\model\bargain\Setting as SettingModel;
use app\common\service\qrcode\bargain\Goods as GoodsPoster;
use think\Cache;
use think\Db;

/**
 * 砍价活动管理
 * Class Active
 * @package app\api\controller\bargain
 */
class Active extends Controller
{

    /**
     * 砍价活动会场列表
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function lists(){
    	return $this->thinkphp_cache_lists();
    }

    // thinkphp Cache (暂时没有找到原因，清除缓存时失败，无法重新缓存)
    public function thinkphp_cache_lists()
    {
    	// return $data = $this->lists2();
    	// return false;
    	
    	$params = $this->request->param();
    	// $expire = 3600 * 24; // 缓存24小时
    	$expire = 3600 * 24; // 缓存24小时
    	
    	// Cache::clear('activeList'.$params['page']);
    	// 获取当前面的缓存
    	$activeList = Cache::get('activeList'.$params['page']);
    	// return json_decode($activeList,true);

    	// 如果不存在
        if(empty($activeList)){
        	$data = $this->lists2();
        	// 缓存数据
        	Cache::tag('activeList')->set('activeList'.$params['page'],json_encode($data),$expire);
        	// 返回数据
        	$data['cache'] = false;
        	return $data;
        }
        // 从缓存中返回数据
        $data = json_decode($activeList,true);
    	$data['cache'] = true;
    	return $data;
    }
    public function lists2(){
    	// 获取砍价活动会场列表
        $model = new ActiveModel;
        $activeList = $model->getHallList();

        $ads = db('ads')->where('type',0)->select();
        return $this->renderSuccess(compact('activeList','ads'));
    }

    /**
     * 砍价活动详情
     * @param $active_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function detail($active_id)
    {
        // 获取砍价活动详情
        $model = new ActiveModel;
        $active = $model->getDetail($active_id);
        if ($active === false) {
            return $this->renderError($model->getError());
        }
        // 标记当前用户是否正在参与
        $task_id = $model->getWhetherPartake($active_id, $this->getUser(false));
        $is_partake = $task_id > 0;
        // 获取商品详情
        $goods = GoodsModel::detail($active['goods_id']);
        
        // 砍价规则
        $setting = SettingModel::getBasic();
    	// print_r($setting);return;

        return $this->renderSuccess(compact('active', 'goods', 'setting', 'is_partake', 'task_id'));
    }

    /**
     * 生成商品海报
     * @param $active_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function poster($active_id)
    {
        // 获取砍价活动详情
        $model = new ActiveModel;
        $active = $model->getDetail($active_id);
        if ($active === false) {
            return $this->renderError($model->getError());
        }
        // 获取商品详情
        $goods = GoodsModel::detail($active['goods_id']);
        // 生成商品海报图
        $Qrcode = new GoodsPoster($active, $goods, $this->getUser(false));
        return $this->renderSuccess([
            'qrcode' => $Qrcode->getImage(),
        ]);
    }

}